<!-- 子公司综管审核 -->
<template>
  <div class="content">
    <dt-search ref="search" />

    <Tabs v-model="curState" type="card" :animated="false" @on-click="onChangeTab">
      <TabPane label="待审核" name="2"></TabPane>
      <TabPane label="已通过" name="3"></TabPane>
      <TabPane label="已驳回" name="4"></TabPane>
    </Tabs>

    <dt-grid ref="table" page :onFetch='onFetch' />

    <!-- 订单附件 -->
    <order-files ref="orderFiles" :data="curItem" :is-edit="isEdit" nodeType="2"
      @success="onSuccess" />
    <!-- 查看录音 -->
    <play-audio ref="playAudio" :tape-ids="curItem.orderTapeIds || null" />
    <!-- 上传录音 -->
    <upload-audio ref="uploadAudio" :id="curItem.orderReviewId || null" @success="onSuccess" />
    <!-- 审核 -->
    <audit-modal ref="audit" :data="curItem" :node-type="2" @success="onSuccess" />
    <!-- 修改订单渠道 -->
    <update-channel ref="updateChannel" :data="curItem" @success="onSuccess" />
  </div>
</template>

<script>
  import orderFiles from '../components/order-files.vue'
  import playAudio from '../components/play-audio.vue'
  import uploadAudio from '../components/upload-audio.vue'
  import auditModal from '../components/audit-modal.vue'
  import updateChannel from '../components/update-channel.vue'
  import Column from './column.js'
  import searchOpt from '../opt/search.js'
  import auditMgr from '@/biz/flowApproval/riskAudit.js'

  export default {
    components: {
      orderFiles,
      playAudio,
      uploadAudio,
      auditModal,
      updateChannel
    },
    data() {
      return {
        curState: '2',
        curItem: {},
        isEdit: false,
      }
    },
    mounted() {
      this.$refs.search.init(this.$refs.table, searchOpt.options(), {
        nodeType: 2,
      })
      this.$refs.table.init(this, new Column(this.curState, this))
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        if (!this.onValidate(data)) {
          return Promise.reject()
        }
        return auditMgr.list({
          ...data,
          state: this.curState,
          companyId: data.companyId && data.companyId.length ? data.companyId[data
            .companyId.length - 1] : null
        })
      },
      onValidate(data) {
        // 校验订单编号
        if (data.orderCode && data.orderCode.length < 4) {
          this.$Message.warning('订单编号至少输入4位')
          return false
        }
        // 首访日期
        if (this.dateDiff(data.firstVisitStart, data.firstVisitEnd) >= 2) {
          this.$Message.warning('首访日期跨度至多两年')
          return false
        }
        // 认购日期
        if (this.dateDiff(data.subscriptionStart, data.subscriptionEnd) >= 2) {
          this.$Message.warning('认购日期跨度至多两年')
          return false
        }
        // 签约日期
        if (this.dateDiff(data.contractStart, data.contractEnd) >= 2) {
          this.$Message.warning('签约日期跨度至多两年')
          return false
        }
        // 审核日期
        if (this.dateDiff(data.reviewStartDate, data.reviewEndDate) >= 2) {
          this.$Message.warning('审核日期跨度至多两年')
          return false
        }
        // 校验手机号
        if (data.customerPhone && data.customerPhone.length < 4) {
          this.$Message.warning('客户手机号至少输入4位')
          return false
        }
        return true
      },
      dateDiff(start, end) {
        if (start && end) {
          return this.$tool.dateDiff(start, end).Y
        } else {
          return 0
        }
      },
      // 查看订单附件
      checkFiles(data) {
        this.isEdit = false
        this.curItem = {
          ...data,
          orderType: data.orderCode.indexOf('RG') >= 0 ? 1 : 2
        }
        this.$refs.orderFiles.visible = true
      },
      // 上传附件
      uploadFiles(data) {
        this.isEdit = true
        this.curItem = {
          ...data,
          orderType: data.orderCode.indexOf('RG') >= 0 ? 1 : 2
        }
        this.$refs.orderFiles.visible = true
      },
      // 查看录音
      checkAudio(data) {
        this.curItem = { ...data }
        this.$refs.playAudio.audioFlag = true
      },
      // 上传录音
      uploadAudio(data) {
        this.curItem = { ...data }
        this.$refs.uploadAudio.visible = true
      },
      // 修改订单渠道
      updateChannel(data) {
        this.curItem = { ...data }
        this.$refs.updateChannel.visible = true
      },
      // 审核
      onAudit(data) {
        this.curItem = { ...data }
        this.$refs.audit.visible = true
      },
      // 提交审核
      submitAudit(data) {
        this.$Modal.confirm({
          title: '请确认是否重新提交审核？',
          onOk: () => {
            auditMgr.reSubmit({
              orderReviewId: data.orderReviewId,
              nodeType: 2
            }).then(res => {
              this.$Message.success('提交成功');
              this.$refs.table.fetch()
            })
          },
          onCancel: () => {}
        })
      },
      onSuccess() {
        this.$refs.table.fetch()
      },
      // 切换tab
      onChangeTab() {
        this.$refs.table.clearFilter()
        this.$refs.table.init(this, new Column(this.curState, this))
        this.$refs.table.fetch(true)
      },
    },
  }
</script>

<style lang="scss" scoped>
  .content {
    padding: 20px;
  }
</style>